/*
 * DeleteMsgs.java
 * 
 * Version information
 * 
 * Mar 31, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.icontact.webapp.struts.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.icontact.hibernate.dataObject.LoginData;
import net.amsoft.iservice.icontact.service.IContact;
import static net.amsoft.iservice.icontact.util.IContactConstants.*;
import net.amsoft.iservice.icontact.webapp.struts.form.DelMsgForm;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * This Action class is used to delete messages from the database which are
 * marked for deletion by the user. The child messages (replies) of a contact
 * request also get deleted, in fact none of the messages is deleted physically
 * from the database but their state is changed to INACTIVE.
 * 
 * @author rahul
 */
public class DeleteMsgs extends BaseAction {

	private static final Logger oLogger = Logger.getLogger(DeleteMsgs.class);

	public ActionForward process(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse)
			throws Exception {
		oLogger.info("process() : entry");
		try {
			LoginData oLoginData = (LoginData) oRequest.getSession(false)
					.getAttribute(SESSION_LOGIN_OBJ);
			ArrayList aAllSyns = oLoginData.getAllSynonyms();
			if (oLoginData.isLoginStatus() == false) {
				return oMapping.findForward(FWD_CHKLOGIN);
			}
			ActionMessages oActMsgs = new ActionErrors();
			boolean delFlag = false;
			DelMsgForm oDelForm = (DelMsgForm) oForm;
			String msgToDel[] = oDelForm.getRequestsToDelete();
			delFlag = IContact.deleteMessages(aAllSyns, msgToDel);
			if (delFlag) {
				oActMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
						MSG_REQ_DELETED));
			} else {
				oActMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
						MSG_REQ_NOT_DELETED));
			}
			saveErrors(oRequest.getSession(), oActMsgs);
			oLogger.info("process() : Returning fwd to " + FWD_SUCCESS);
		} catch (Exception e) {
			oLogger.warn("process() : Exception : " + e.getMessage());
		}
		return oMapping.findForward(FWD_SUCCESS);
	}
}
